package com.jun.send.job;

import com.alibaba.fastjson.JSON;
import com.jun.send.model.WpDataInfo;
import com.jun.send.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Service
public class SendMessageJob {
    @Resource
    private WebSocketServer webSocketServer;

    @Scheduled(cron = "0 */2 * * * ?")
    public void sendMessage() {
        ConcurrentHashMap<WebSocketServer, String> webSocketMap = WebSocketServer.webSocketMap;
        if (webSocketMap.size() <= 0) {
            log.info("没有session，不进行处理，不进行推送数据");
            return;
        }

        WpDataInfo wpDataInfo = new WpDataInfo();
        wpDataInfo.setDeviceNo("34-ME-01");
        wpDataInfo.setCreateTime("2022-08-17 00:00:00");
        webSocketServer.sendMessage(JSON.toJSONString(wpDataInfo), "wp");
        log.info("发送的数据成功");
    }

}
